package www.lagou.app;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import www.lagou.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;

public class Homework1 {
    public static void main(String[] args) {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql1 = "select balance from account where card = '1122334455';";
        Double balance = null;
        try {
            balance = qr.query(sql1, new ScalarHandler<Double>());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(balance.doubleValue() < 5000){
            System.out.println("余额不足");
        }else{
            String sql2 = "update account set balance = balance - 5000 where card = '1122334455';";
            String sql3 = "update account set balance = balance + 5000 where card = '55443332211';";
            String sql4 = "insert into transaction (cardid, tratype, tramoney, tradate) values('1122334455', '转出', 5000, ?)";
            try {
                qr.update(sql2);
                qr.update(sql3);
                qr.update(sql4, LocalDateTime.now());
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }
}
